//
//  WJNewFeatureView.swift
//  新浪微博swift
//
//  Created by WJ-Jason on 2017/5/6.
//  Copyright © 2017年 WJ-Jason. All rights reserved.
//

import UIKit

class WJNewFeatureView: UIView {
    
    @IBOutlet weak var srollView: UIScrollView!
    
    @IBOutlet weak var enterButton: UIButton!

    @IBOutlet weak var pageControl: UIPageControl!
    
    // 进入微博
    @IBAction func enterStatus() {
        removeFromSuperview()
    }
    
    
    class func newFeatureView () -> WJNewFeatureView {
        let nib = UINib(nibName: "WJNewFeatureView", bundle: nil)
        
        let v = nib.instantiate(withOwner: nil, options: nil)[0] as! WJNewFeatureView
        
        // 从xib加载的视图,默认是600*600的
        v.frame = UIScreen.main.bounds
        
        return v
    }
    
    override func awakeFromNib() {
        
        // 如果使用自动布局设置的界面，从 XIB 加载默认是 600 * 600 大小
        // 添加 4 个图像视图
        
        let count = 4
        let rect = UIScreen.main.bounds
        
        for i in 0..<count {
            let imageName = "new_feature_\(i + 1)"
            
            let iv = UIImageView(image: UIImage(named: imageName))
            
            // 设置大小
            iv.frame = rect.offsetBy(dx: CGFloat(i) * rect.width, dy: 0)
            
            srollView.addSubview(iv)
        }
        
        // 指定scrollView 的属性
        srollView.contentSize = CGSize(width: CGFloat(count+1) * rect.width, height: rect.height)
        srollView.bounces = false
        
        srollView.isPagingEnabled = true
        
        srollView.showsVerticalScrollIndicator = false
        
        srollView.showsHorizontalScrollIndicator = false
        
        srollView.delegate = self
        
        // 隐藏按钮
        enterButton.isHidden = true
    
    }
}

extension WJNewFeatureView : UIScrollViewDelegate {
    func scrollViewDidEndDecelerating(_ scrollView: UIScrollView) {
        // 1. 滚动到最后一屏,让视图删除
        let page = srollView.contentOffset.x / srollView.bounds.width
        
        print(page)
        
        // 2. 判断是否最后一页
        if page == CGFloat(scrollView.subviews.count) {
            
            removeFromSuperview()
        }
        
        // 3.如果是倒数第二页,显示按钮
        enterButton.isHidden = (page != CGFloat(srollView.subviews.count - 1))
    }
    
    func scrollViewDidScroll(_ scrollView: UIScrollView) {
        
        // 0. 一旦滚动隐藏按钮
        enterButton.isHidden = true
        
        // 1. 计算当前的偏移量
        let page = Int(scrollView.contentOffset.x / scrollView.bounds.width + 0.5)
        
        // 2. 设置分页控件
        pageControl.currentPage = page
        
        // 3. 分页控件的隐藏
        pageControl.isHidden = (page == scrollView.subviews.count)
    }

}
